home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / text / cmanual.lzh / ACM2.lzh / Menus / Example4.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  12KB  |  320 lines

  1. /* Example4                                                             */
  2. /* This program opens a normal window to which we connect a menu strip. */
  3. /* The menu will look like this:                                        */
  4. /*                                                                      */
  5. /* Status                                                               */
  6. /* --------------                                                       */
  7. /* | v Readmode | (ghosted)                                             */
  8. /* |   Editmode |                                                       */
  9. /* --------------                                                       */
  10. /*                                                                      */
  11. /* The Readmode item is selected and ghosted, and when the user selects */
  12. /* the Editmode item, it will become disabled (ghosted) while the read- */
  13. /* mode item will be enabled (not ghosted). This means that if the      */
  14. /* program is in "readmode", the user should only be able to chose the  */
  15. /* "editmode", and v.v.                                                 */ 
  16. /*                                                                      */
  17. /* The purpose with this program is to show how you can use the OnMenu  */
  18. /* and OffMenu functions inorder to make an "user-friendly interface".  */
  19.  
  20.  
  21.  
  22. #include <intuition/intuition.h>
  23.  
  24.  
  25.  
  26. struct IntuitionBase *IntuitionBase;
  27.  
  28.  
  29.  
  30. /*************************************************************************/
  31. /*                       E D I T M O D E   I T E M                       */
  32. /*************************************************************************/
  33.  
  34. /* The text for the editmode item: */
  35. struct IntuiText my_editmode_text=
  36. {
  37.   2,          /* FrontPen, black. */
  38.   0,          /* BackPen, not used since JAM1. */
  39.   JAM1,       /* DrawMode, do not change the background. */
  40.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  41.               /* This will leave enough space for the check mark. */
  42.   1,          /* TopEdge, 1 line down. */
  43.   NULL,       /* TextAttr, default font. */
  44.   "Editmode", /* IText, the string. */
  45.   NULL        /* NextItem, no link to other IntuiText structures. */
  46. };
  47.  
  48. /* The MenuItem structure for the editmode item: */
  49. struct MenuItem my_editmode_item=
  50. {
  51.   NULL,            /* NextItem, last item in the list. */
  52.   0,               /* LeftEdge, 0 pixels out. */
  53.   10,              /* TopEdge, 10 lines down. */
  54.   150,             /* Width, 150 pixels wide. */
  55.   10,              /* Height, 10 lines high. */
  56.   ITEMTEXT|        /* Flags, render this item with text. */
  57.   ITEMENABLED|     /*        this item will be enabled. */
  58.   CHECKIT|         /*        it is an attribute item. */
  59.   HIGHCOMP,        /*        complement the colours when highlihted. */
  60.   0xFFFFFFFD,      /* MutualExclude, mutualexclude all items except the */
  61.                    /*                second (this) one. */
  62.   (APTR) &my_editmode_text, /* ItemFill, pointer to the text. */
  63.   NULL,            /* SelectFill, nothing since we complement the col. */
  64.   0,               /* Command, not accessable from the keyboard. */
  65.   NULL,            /* SubItem, ignored by Intuition. */
  66.   MENUNULL,        /* NextSelect, no items selected. */
  67. };
  68.  
  69.  
  70.  
  71. /*************************************************************************/
  72. /*                       R E A D M O D E   I T E M                       */
  73. /*************************************************************************/
  74.  
  75. /* The text for the readmode item: */
  76. struct IntuiText my_readmode_text=
  77. {
  78.   2,          /* FrontPen, black. */
  79.   0,          /* BackPen, not used since JAM1. */
  80.   JAM1,       /* DrawMode, do not change the background. */
  81.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  82.               /* This will leave enough space for the check mark. */
  83.   1,          /* TopEdge, 1 line down. */
  84.   NULL,       /* TextAttr, default font. */
  85.   "Readmode", /* IText, the string. */
  86.   NULL        /* NextItem, no link to other IntuiText structures. */
  87. };
  88.  
  89. /* The MenuItem structure for the readmode item: */
  90. struct MenuItem my_readmode_item=
  91. {
  92.   &my_editmode_item, /* NextItem, pointer to the second (edit) item. */
  93.   0,                 /* LeftEdge, 0 pixels out. */
  94.   0,                 /* TopEdge, 0 lines down. */
  95.   150,               /* Width, 150 pixels wide. */
  96.   10,                /* Height, 10 lines high. */
  97.   ITEMTEXT|          /* Flags, render this item with text. */
  98.                      /*        this item will be disabled. */
  99.   CHECKIT|           /*        it is an attribute item. */
  100.   CHECKED|           /*        this item is initially selected. */
  101.   HIGHCOMP,          /*        complement the colours when highlihted. */
  102.   0xFFFFFFFE,        /* MutualExclude, mutualexclude all items except the */
  103.                      /*                first (this) one. */
  104.   (APTR) &my_readmode_text, /* ItemFill, pointer to the text. */
  105.   NULL,              /* SelectFill, nothing since we complement the col. */
  106.   0,                 /* Command, not accessable from the keyboard. */
  107.   NULL,              /* SubItem, ignored by Intuition. */
  108.   MENUNULL,          /* NextSelect, no items selected. */
  109. };
  110.  
  111.  
  112.  
  113. /*************************************************************************/
  114. /*                              M E N U                                  */
  115. /*************************************************************************/
  116.  
  117. /* The Menu structure for the first (and only) menu: */
  118. struct Menu my_menu=
  119. {
  120.   NULL,             /* NextMenu, no more menu structures. */
  121.   0,                /* LeftEdge, left corner. */
  122.   0,                /* TopEdge, for the moment ignored by Intuition. */
  123.   50,               /* Width, 50 pixels wide. */
  124.   0,                /* Height, for the moment ignored by Intuition. */
  125.   MENUENABLED,      /* Flags, this menu will be enabled. */
  126.   "Status",         /* MenuName, the string. */
  127.   &my_readmode_item /* FirstItem, pointer to the first item in the list. */
  128. };
  129.  
  130.  
  131.  
  132. /* Declare a pointer to a Window structure: */ 
  133. struct Window *my_window;
  134.  
  135. /* Declare and initialize your NewWindow structure: */
  136. struct NewWindow my_new_window=
  137. {
  138.   50,            /* LeftEdge    x position of the window. */
  139.   25,            /* TopEdge     y positio of the window. */
  140.   250,           /* Width       250 pixels wide. */
  141.   100,           /* Height      100 lines high. */
  142.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  143.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  144.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  145.                  /*             user has selected the Close window gad. */
  146.   MENUPICK,
  147.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  148.   WINDOWCLOSE|   /*             Close Gadget. */
  149.   WINDOWDRAG|    /*             Drag gadget. */
  150.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  151.   WINDOWSIZING|  /*             Sizing Gadget. */
  152.   ACTIVATE,      /*             The window should be Active when opened. */
  153.   NULL,          /* FirstGadget No Custom gadgets. */
  154.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  155.   "Read or Edit", /* Title      Title of the window. */
  156.   NULL,          /* Screen      Connected to the Workbench Screen. */
  157.   NULL,          /* BitMap      No Custom BitMap. */
  158.   80,            /* MinWidth    We will not allow the window to become */
  159.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  160.   300,           /* MaxWidth    than 300 x 200. */
  161.   200,           /* MaxHeight */
  162.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  163. };
  164.  
  165.  
  166.  
  167. main()
  168. {
  169.   /* Boolean variable used for the while loop: */
  170.   BOOL close_me;
  171.  
  172.   /* Declare a variable in which we will store the IDCMP flag: */
  173.   ULONG class;
  174.   
  175.   /* If we recieve a MENUPICK event, the Code field of the message */
  176.   /* structure will contain the menu number of the first selected item. */
  177.   /* Declare a variable to store the Code value in, and two extra menu */
  178.   /* number variables: */
  179.   USHORT code, menu_number, number;
  180.   
  181.   /* Declare a MenuItem pointer: */
  182.   struct MenuItem *item;
  183.   
  184.   /* Declare a pointer to an IntuiMessage structure: */
  185.   struct IntuiMessage *my_message;
  186.  
  187.  
  188.  
  189.   /* Before we can use Intuition we need to open the Intuition Library: */
  190.   IntuitionBase = (struct IntuitionBase *)
  191.     OpenLibrary( "intuition.library", 0 );
  192.   
  193.   if( IntuitionBase == NULL )
  194.     exit(); /* Could NOT open the Intuition Library! */
  195.  
  196.  
  197.  
  198.   /* We will now try to open the window: */
  199.   my_window = (struct Window *) OpenWindow( &my_new_window );
  200.   
  201.   /* Have we opened the window succesfully? */
  202.   if(my_window == NULL)
  203.   {
  204.     /* Could NOT open the Window! */
  205.     
  206.     /* Close the Intuition Library since we have opened it: */
  207.     CloseLibrary( IntuitionBase );
  208.  
  209.     exit();  
  210.   }
  211.  
  212.  
  213.  
  214.   /* We have opened the window, and everything seems to be OK. */
  215.  
  216.  
  217.  
  218.   SetMenuStrip( my_window, &my_menu );
  219.   printf("Menustrip connected to window!\n");
  220.  
  221.  
  222.   close_me = FALSE;
  223.  
  224.   /* Stay in the while loop until the user has selected the Close window */
  225.   /* gadget: */
  226.   while( close_me == FALSE )
  227.   {
  228.     /* Wait until we have recieved a message: */
  229.     Wait( 1 << my_window->UserPort->mp_SigBit );
  230.  
  231.     /* As long as we collect messages sucessfully we stay in the loop: */
  232.     while(my_message=(struct IntuiMessage *) GetMsg( my_window->UserPort ))
  233.     {
  234.       /* After we have collected the message we can read it, and save any */
  235.       /* important values which we maybe want to check later: */
  236.       class = my_message->Class;
  237.       code = my_message->Code;
  238.  
  239.  
  240.       /* After we have read it we reply as fast as possible: */
  241.       /* REMEMBER! Do never try to read a message after you have replied! */
  242.       /* Some other process has maybe changed it. */
  243.       ReplyMsg( my_message );
  244.  
  245.       /* Check which IDCMP flag was sent: */
  246.       if( class == CLOSEWINDOW )
  247.         close_me=TRUE; /* The user selected the Close window gadget! */  
  248.  
  249.       if(class == MENUPICK)
  250.       {
  251.         printf("\nMenu pick!\n");
  252.         menu_number = code;
  253.         
  254.         while( menu_number != MENUNULL )
  255.         {
  256.           /* Get the address of the item: */
  257.           item = (struct MenuItem *) ItemAddress( &my_menu, menu_number );
  258.  
  259.  
  260.  
  261.           /* Check which item was selected: */
  262.           if( item == &my_readmode_item )
  263.           {
  264.             /* The Readmode (first) item was selected! */
  265.             printf("We are now in READMODE!\n");
  266.             
  267.             /* Disable the Readmode item: */
  268.             number = SHIFTMENU( 0 ) + SHIFTITEM( 0 ) + SHIFTSUB( NOSUB );
  269.             /*       first menu       first item       no subitem. */
  270.             OffMenu( my_window, number );
  271.  
  272.             /* Enable the Editmode item: */
  273.             number = SHIFTMENU( 0 ) + SHIFTITEM( 1 ) + SHIFTSUB( NOSUB );
  274.             /*       first menu       second item      no subitem. */
  275.             OnMenu( my_window, number );
  276.           }
  277.  
  278.           if( item == &my_editmode_item )
  279.           {
  280.             /* The Editmode (second) item was selected! */
  281.             printf("We are now in EDITMODE!\n");
  282.             
  283.             /* Disable the Editmode item: */
  284.             number = SHIFTMENU( 0 ) + SHIFTITEM( 1 ) + SHIFTSUB( NOSUB );
  285.             /*       first menu       second item      no subitem. */
  286.             OffMenu( my_window, number );
  287.  
  288.             /* Enable the Readmode item: */
  289.             number = SHIFTMENU( 0 ) + SHIFTITEM( 0 ) + SHIFTSUB( NOSUB );
  290.             /*       first menu       first item       no subitem. */
  291.             OnMenu( my_window, number );
  292.           }
  293.  
  294.  
  295.  
  296.           /* Get the following item's menu number: */
  297.           menu_number = item->NextSelect;
  298.         }
  299.       }
  300.     }
  301.   }
  302.  
  303.  
  304.  
  305.   printf("Menustrip removed from window!\n");
  306.   ClearMenuStrip( my_window );
  307.  
  308.  
  309.  
  310.   /* Close the window: */
  311.   CloseWindow( my_window );
  312.  
  313.  
  314.  
  315.   /* Close the Intuition Library since we have opened it: */
  316.   CloseLibrary( IntuitionBase );
  317.   
  318.   /* THE END */
  319. }
  320.